﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.IO;

namespace HPortal.Backend
{
    public partial class DSChunhiem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    cboDonvi.DataSource = DatabaseHelper.Select("Select DonviID,Ten From KH_Donvi ORDER BY Ten DESC");
                    cboDonvi.DataTextField = "Ten";
                    cboDonvi.DataValueField = "DonviID";
                    cboDonvi.DataBind();

                    grvManager_DataBind();
                }
            }
        }
        private void grvManager_DataBind()
        {
            DataTable la = DatabaseHelper.Select("Exec [KH_TimkiemChunhiem] N" + DatabaseHelper.EscapeQuote(txtKeyword.Text));
            grvChunhiem.DataSource = la;
            grvChunhiem.DataBind();
        }

        protected void lnkChonTiendo_Click(object sender, EventArgs e)
        {
            string ChunhiemID = ((LinkButton)sender).CommandArgument;
            DataTable t = DatabaseHelper.Select("Select * From KH_Nguoidung Where NguoidungID=" + ChunhiemID);
            txtHoten.Text = t.Rows[0]["Hoten"].ToString();
            txtTendangnhap.Text = t.Rows[0]["Tendangnhap"].ToString();
            cboDonvi.SelectedValue = t.Rows[0]["DonviID"].ToString();
            cmdEditTiendo.Enabled = true;
            cmdEditTiendo.CommandArgument = ChunhiemID;
        }

        protected void lnkXoaTiendo_Click(object sender, EventArgs e)
        {
            string ChunhiemID = ((LinkButton)sender).CommandArgument;
            DatabaseHelper.Execute("Delete From KH_Nguoidung Where NguoidungID=" + ChunhiemID);
            txtHoten.Text = "";
            txtTendangnhap.Text = "";
            txtKeyword.Text = "";

            grvManager_DataBind();
        }

        protected void cmdEditTiendo_Click(object sender, EventArgs e)
        {
            string ChunhiemID = ((Button)sender).CommandArgument;
            DatabaseHelper.Select("Update KH_Nguoidung Set Hoten=" + DatabaseHelper.EscapeQuoteUnicode(txtHoten.Text) + ",Tendangnhap=" + DatabaseHelper.EscapeQuoteUnicode(txtTendangnhap.Text) + ",DonviID=" + cboDonvi.SelectedValue + " Where NguoidungID=" + ChunhiemID);

            grvManager_DataBind();
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            grvManager_DataBind();
        }

        protected void cmdThemTiendo_Click(object sender, EventArgs e)
        {
            DatabaseHelper.Select("Exec [KH_InsertNguoidung] " + DatabaseHelper.EscapeQuoteUnicode(txtHoten.Text) + "," + DatabaseHelper.EscapeQuoteUnicode(txtTendangnhap.Text) + "," + cboDonvi.SelectedValue);
            grvManager_DataBind();
 
        }

        protected void grvChunhiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvChunhiem.PageIndex = e.NewPageIndex;
            grvManager_DataBind();
        }
    }
}